﻿/*----------------------------------------------------------------
// Copyright (C) 2013 上海汽车集团股份有限公司
// 版权所有。
//
// 文件名：DropDownList.js
// 文件功能描述：下拉框级联关系处理文件
//              文件依赖：jquery-1.8.0.min.js、
//                       Common.js、 
//                       Window.js、
//                       jquery.linq.min.js
//                       jquery.easyui.min.js
//
//
// 创建标识：  曹珊 2013/8/16
//
// 修改标识：
// 修改描述：
//
//----------------------------------------------------------------*/

var comboType = { None: "0", Query: "1", Edit: "2",Add: "3" };
function buildComboData(data, type) {
    var Data = [];
    if (data.Content.length > 0)
        Data = data.Content.concat();
    if (type != comboType.None) {
        var firstItem = { ID: "", Text: "全选" };
        Data.insert(0, firstItem);
        if (type == comboType.Edit) {
            Data[0].Text = "请选择";
        }
        if (type == comboType.Add) {
            Data[0].Text = "--";
        }
    }
    return Data;
}

//初始化下拉框控件
function CreateCombobox(v) {
    if (!isNullOrEmpty(v)) {
        if ($.type(v) == "array") {
            $(v).each(function (i, item) {
                $("#" + item).combobox({
                    valueField: "ID",
                    textField: "Text"
                });
            });
        }
        if ($.type(v) == "string") {
            $("#" + v).combobox({
                valueField: "ID",
                textField: "Text"
            });
        }
    } else {
        jqAlert("请指定Combobox的ID属性！");
    }
}


//清空数据或重新加载下拉框  参数：控件ID, 数据， 默认值， 类型
function resetOrLoad(id, data, defValue, type) {
    if ($("#" + id).length == 0) return false;

    if (isNullOrEmpty(type)) {
        type = comboType.None;
    }

    var d = { Content: [] };
    if (isNullOrEmpty(data.Content)) {
        d.Content = data;
    }
    else {
        d = data;
    }

    var Data = buildComboData(d, type);

    $("#" + id).combobox("clear");
    $("#" + id).combobox("loadData", []);
    if (!isNullOrEmpty(Data)) {
        $("#" + id).combobox("loadData", Data);
    }
    if (!isNullOrEmpty(defValue)) {
        $("#" + id).combobox("setValue", defValue);
    }
}

//无级联关系的下拉框初始化
function InitSingleCombobox(comboOption) {
    var defaults = {
        type: comboType.Query,
        container: "",
        combosData: [],
        defaultValue: "",
        remoteUrl: '',
        autoLoad: true, //没有初始数据，是否直接加载remoteUrl的数据
        param: null, //参数 {param1:function(){return '123';},param2:function(){return 'abc';}}
        onSelectFn: null
    };
    var options = $.extend(defaults, comboOption);

    if (!isNullOrEmpty(options.container)) {
        var data = options.combosData;
        //var Data = buildComboData(data, options.type);
        resetOrLoad(options.container, data, options.defaultValue, options.type);
    }
    if (!isNullOrEmpty(options.container)) {
        $("#" + options.container).combobox("select", options.defaultValue);
    }
    if (!isNullOrEmpty(options.remoteUrl)) {//动态加载远程数据
        var param = null;
        if (!isNullOrEmpty(options.param)) {
            param = options.param;
            for (var item in param) {
                if (!($.type(param[item]) == 'function')) {
                    jqAlert("参数param的属性类型不是预期的类型(function)!");
                    return;
                }
            }
        }

        options.remoteUrl = setUrlParam(options.remoteUrl, 'remote', 'true');
        $.extend($("#" + options.container).combobox("options"), {
            url: options.remoteUrl,
            mode: 'remote',
            filter: function (q, row) {
                var opts = $(this).combobox('options');
                return row[opts.textField].indexOf(q) == 0;
            },
            param: param
        });
        //如果没有初始数据，直接加载remoteUrl的数据
        if (options.autoLoad) {
            if (isNullOrEmpty(options.combosData) || options.combosData.length == 0) {
                $("#" + options.container).combobox("reload", options.remoteUrl);
            }
        }
    }
    if (options.onSelectFn != null && $.type(options.onSelectFn) == "function") {
        $("#" + options.container).combobox("options").onSelect = options.onSelectFn;
    }
}

//工厂数据下拉框
function MakeDivisionCombobox(comboOption) {
    var defaults =
    {
        type: comboType.Query,
        container: {
            division: ''
        },
        combosData: [],
        defaultValue: ''
    };
    var options = $.extend(defaults, comboOption);
    //工厂
    if (!isNullOrEmpty(options.container.division)) {
        //$("#" + options.container.division).combobox('destroy');
        $("#" + options.container.division).combobox({
            valueField: "ID",
            textField: "Text",
            editable: false
        });

        $("#" + options.container.division).data({ "isDivision": true });
        var division = authorityData.concat();
        //division = buildComboData(division, options.type);

        $("#" + options.container.division).combobox("loadData", division);

        if (!isNullOrEmpty(options.defaultValue.division)) {
            $("#" + options.container.division).combobox('select', options.defaultValue.division);
            $("#" + options.container.division).data({ "DefaultDivision": options.defaultValue.division });
        } else {
            var def_v = '';
            $(division).each(function (i, item) {
                if (item["Extend"] == "true" || item["Extend"] == true) {
                    def_v = item["ID"];
                    $("#" + options.container.division).data({ "DefaultDivision": def_v });
                    return false;
                }
            });
            $("#" + options.container.division).combobox('select', def_v);
        }
    }
}




/****************************************************/
//工厂、车间、仓库、库区、库位 级联
function MakeDFWZLCascadeCombobox(comboOption) {
    var defaults =
    {
        type: comboType.Query,
        container: {
            division: '',
            facility: '',
            warehouse: '',
            zone: '',
            location: ''
        },
        combosData: {},
        defaultValue: {}
    };
    var options = $.extend(defaults, comboOption);


    //车间
    if (!isNullOrEmpty(options.container.facility)) {
        //加载车间数据
        if (!isNullOrEmpty(options.combosData.facility)) {
            resetOrLoad(options.container.facility, options.combosData.facility);
        }

        //选中默认值
        if (!isNullOrEmpty(options.defaultValue.facility)) {
            $("#" + options.container.facility).combobox('select', options.defaultValue.facility);
        }

        //创建级联关系，工厂级联车间
        $("#" + options.container.division).combobox('options').onSelect = function (r) {
            var d_v = $(this).combobox('getValue');
            var url = "/Server/DropDownListResult.aspx?Method=FacilityCombobox";
            url = setUrlParam(url, "divisionID", d_v);
            url = setUrlParam(url, "Type", options.type);
            url = GetRandURL(url);
            $("#" + options.container.facility).combobox("reload", url);
            //清空
            resetOrLoad(options.container.facility);
            resetOrLoad(options.container.warehouse);
            resetOrLoad(options.container.zone);
            resetOrLoad(options.container.location);
        };
    }

    //仓库
    if (!isNullOrEmpty(options.container.warehouse)) {
        //加载仓库数据
        if (!isNullOrEmpty(options.combosData.warehouse)) {
            resetOrLoad(options.container.warehouse, options.combosData.warehouse);
        }

        //选中默认值
        if (!isNullOrEmpty(options.defaultValue.warehouse)) {
            $("#" + options.container.warehouse).combobox('select', options.defaultValue.warehouse);
        }

        //创建级联关系,工厂、车间级联仓库
        $("#" + options.container.facility).combobox('options').onSelect = function (r) {
            var f_v = $(this).combobox('getValue');
            var d_v = $("#" + options.container.division).combobox('getValue');
            var url = "/Server/DropDownListResult.aspx?Method=WarehouseCombobox";
            url = setUrlParam(url, "divisionID", d_v);
            url = setUrlParam(url, "facilityID", f_v);
            url = setUrlParam(url, "Type", options.type);
            url = GetRandURL(url);
            $("#" + options.container.warehouse).combobox("reload", url);
            //清空 
            resetOrLoad(options.container.warehouse);
            resetOrLoad(options.container.zone);
            resetOrLoad(options.container.location);
        };
    }

    //库区
    if (!isNullOrEmpty(options.container.zone)) {
        //加载库位数据
        if (!isNullOrEmpty(options.combosData.zone)) {
            resetOrLoad(options.container.zone, options.combosData.zone);
        }

        //选中默认值
        if (!isNullOrEmpty(options.defaultValue.zone)) {
            $("#" + options.container.zone).combobox('select', options.defaultValue.zone);
        }

        //工厂、车间级联仓库
        $("#" + options.container.warehouse).combobox('options').onSelect = function (r) {
            var w_v = $(this).combobox('getValue');
            var d_v = $("#" + options.container.division).combobox('getValue');
            var f_v = $("#" + options.container.facility).combobox('getValue');
            var url = "/Server/DropDownListResult.aspx?Method=ZoneCombobox";
            url = setUrlParam(url, "divisionID", d_v);
            url = setUrlParam(url, "facilityID", f_v);
            url = setUrlParam(url, "warehouseID", w_v);
            url = setUrlParam(url, "Type", options.type);
            url = GetRandURL(url);
            $("#" + options.container.zone).combobox("reload", url);
            //清空  
            resetOrLoad(options.container.zone);
            resetOrLoad(options.container.location);
        };
    }

    //库位
    if (!isNullOrEmpty(options.container.location)) {
        //加载库位数据
        if (!isNullOrEmpty(options.combosData.location)) {
            resetOrLoad(options.container.location, options.combosData.location);
        }

        //选中默认值
        if (!isNullOrEmpty(options.defaultValue.location)) {
            $("#" + options.container.location).combobox('select', options.defaultValue.location);
        }

        //工厂、车间级联仓库
        $("#" + options.container.zone).combobox('options').onSelect = function (r) {
            var z_v = $(this).combobox('getValue');
            var d_v = $("#" + options.container.division).combobox('getValue');
            var f_v = $("#" + options.container.facility).combobox('getValue');
            var w_v = $("#" + options.container.warehouse).combobox('getValue');
            var url = "/Server/DropDownListResult.aspx?Method=LocationCombobox";
            url = setUrlParam(url, "divisionID", d_v);
            url = setUrlParam(url, "facilityID", f_v);
            url = setUrlParam(url, "warehouseID", w_v);
            url = setUrlParam(url, "zoneID", z_v);
            url = setUrlParam(url, "Type", options.type);
            url = GetRandURL(url);
            $("#" + options.container.location).combobox("reload", url);
            //清空 
            resetOrLoad(options.container.location);
        };
    }
    //工厂
    MakeDivisionCombobox(options);
}




/****************************************************/
//工厂、车间、产线 级联
function MakeDFLCascadeCombobox(comboOption) {
    var defaults =
    {
        type: comboType.Query,
        container: {
            division: '',
            facility: '',
            line: ''
        },
        combosData: {},
        defaultValue: {}
    };
    var options = $.extend(defaults, comboOption);


    //车间
    if (!isNullOrEmpty(options.container.facility)) {
        //加载车间数据
        if (!isNullOrEmpty(options.combosData.facility)) {
            resetOrLoad(options.container.facility, options.combosData.facility);
        }

        //选中默认值
        if (!isNullOrEmpty(options.defaultValue.facility)) {
            $("#" + options.container.facility).combobox('select', options.defaultValue.facility);
        }

        //创建级联关系，工厂级联车间
        $("#" + options.container.division).combobox('options').onSelect = function (r) {
            var d_v = $(this).combobox('getValue');
            var url = "/Server/DropDownListResult.aspx?Method=FacilityCombobox";
            url = setUrlParam(url, "divisionID", d_v);
            url = setUrlParam(url, "Type", options.type);
            url = GetRandURL(url);
            $("#" + options.container.facility).combobox("reload", url);
            //清空
            resetOrLoad(options.container.facility);
            resetOrLoad(options.container.line);
        };
    }

    //产线
    if (!isNullOrEmpty(options.container.line)) {
        //加载仓库数据
        if (!isNullOrEmpty(options.combosData.line)) {
            resetOrLoad(options.container.line, options.combosData.line);
        }

        //选中默认值
        if (!isNullOrEmpty(options.defaultValue.line)) {
            $("#" + options.container.line).combobox('select', options.defaultValue.line);
        }

        //创建级联关系,工厂、车间级联仓库
        $("#" + options.container.facility).combobox('options').onSelect = function (r) {
            var f_v = $(this).combobox('getValue');
            var d_v = $("#" + options.container.division).combobox('getValue');
            var url = "/Server/DropDownListResult.aspx?Method=LineCombobox";
            url = setUrlParam(url, "divisionID", d_v);
            url = setUrlParam(url, "facilityID", f_v);
            url = setUrlParam(url, "Type", options.type);
            url = GetRandURL(url);
            $("#" + options.container.line).combobox("reload", url);
            //清空  
            resetOrLoad(options.container.line);
        };
    }
    //工厂
    MakeDivisionCombobox(options);
}


//工厂、车间、仓库、库区、库位, 工厂、车间、工位级联
function MakeDFWZL_DFWCascadeCombobox(comboOption) {
    var defaults =
    {
        type: comboType.Query,
        container: {
            division: '',
            facility: '',
            warehouse: '',
            zone: '',
            location: '',
            workcenter: ''
        },
        combosData: {},
        defaultValue: {}
    };
    var options = $.extend(defaults, comboOption);


    //车间
    if (!isNullOrEmpty(options.container.facility)) {
        //加载车间数据
        if (!isNullOrEmpty(options.combosData.facility)) {
            resetOrLoad(options.container.facility, options.combosData.facility);
        }

        //选中默认值
        if (!isNullOrEmpty(options.defaultValue.facility)) {
            $("#" + options.container.facility).combobox('select', options.defaultValue.facility);
        }

        //创建级联关系，工厂级联车间
        $("#" + options.container.division).combobox('options').onSelect = function (r) {
            var d_v = $(this).combobox('getValue');
            var url = "/Server/DropDownListResult.aspx?Method=FacilityCombobox";
            url = setUrlParam(url, "divisionID", d_v);
            url = setUrlParam(url, "Type", options.type);
            url = GetRandURL(url);
            $("#" + options.container.facility).combobox("reload", url);
            //清空
            resetOrLoad(options.container.facility);
            resetOrLoad(options.container.warehouse);
            resetOrLoad(options.container.zone);
            resetOrLoad(options.container.location);
            resetOrLoad(options.container.workcenter);
        };
    }

    //仓库
    if (!isNullOrEmpty(options.container.warehouse)) {
        //加载仓库数据
        if (!isNullOrEmpty(options.combosData.warehouse)) {
            resetOrLoad(options.container.warehouse, options.combosData.warehouse);
        }

        //选中默认值
        if (!isNullOrEmpty(options.defaultValue.warehouse)) {
            $("#" + options.container.warehouse).combobox('select', options.defaultValue.warehouse);
        }

        //创建级联关系,工厂、车间级联仓库
        $("#" + options.container.facility).combobox('options').onSelect = function (r) {
            var f_v = $(this).combobox('getValue');
            var d_v = $("#" + options.container.division).combobox('getValue');
            var url = "/Server/DropDownListResult.aspx?Method=WarehouseCombobox";
            url = setUrlParam(url, "divisionID", d_v);
            url = setUrlParam(url, "facilityID", f_v);
            url = setUrlParam(url, "Type", options.type);
            url = GetRandURL(url);
            $("#" + options.container.warehouse).combobox("reload", url);
            //清空 
            resetOrLoad(options.container.warehouse);
            resetOrLoad(options.container.zone);
            resetOrLoad(options.container.location);
        };
    }

    //库区
    if (!isNullOrEmpty(options.container.zone)) {
        //加载库位数据
        if (!isNullOrEmpty(options.combosData.zone)) {
            resetOrLoad(options.container.zone, options.combosData.zone);
        }

        //选中默认值
        if (!isNullOrEmpty(options.defaultValue.zone)) {
            $("#" + options.container.zone).combobox('select', options.defaultValue.zone);
        }

        //工厂、车间级联仓库
        $("#" + options.container.warehouse).combobox('options').onSelect = function (r) {
            var w_v = $(this).combobox('getValue');
            var d_v = $("#" + options.container.division).combobox('getValue');
            var f_v = $("#" + options.container.facility).combobox('getValue');
            var url = "/Server/DropDownListResult.aspx?Method=ZoneCombobox";
            url = setUrlParam(url, "divisionID", d_v);
            url = setUrlParam(url, "facilityID", f_v);
            url = setUrlParam(url, "warehouseID", w_v);
            url = setUrlParam(url, "Type", options.type);
            url = GetRandURL(url);
            $("#" + options.container.zone).combobox("reload", url);
            //清空  
            resetOrLoad(options.container.zone);
            resetOrLoad(options.container.location);
        };
    }

    //库位
    if (!isNullOrEmpty(options.container.location)) {
        //加载库位数据
        if (!isNullOrEmpty(options.combosData.location)) {
            resetOrLoad(options.container.location, options.combosData.location);
        }

        //选中默认值
        if (!isNullOrEmpty(options.defaultValue.location)) {
            $("#" + options.container.location).combobox('select', options.defaultValue.location);
        }

        //工厂、车间级联仓库
        $("#" + options.container.zone).combobox('options').onSelect = function (r) {
            var z_v = $(this).combobox('getValue');
            var d_v = $("#" + options.container.division).combobox('getValue');
            var f_v = $("#" + options.container.facility).combobox('getValue');
            var w_v = $("#" + options.container.warehouse).combobox('getValue');
            var url = "/Server/DropDownListResult.aspx?Method=LocationCombobox";
            url = setUrlParam(url, "divisionID", d_v);
            url = setUrlParam(url, "facilityID", f_v);
            url = setUrlParam(url, "warehouseID", w_v);
            url = setUrlParam(url, "zoneID", z_v);
            url = setUrlParam(url, "Type", options.type);
            url = GetRandURL(url);
            $("#" + options.container.location).combobox("reload", url);
            //清空 
            resetOrLoad(options.container.location);
        };
    }

    //工位
    if (!isNullOrEmpty(options.container.workcenter)) {
        //加载库位数据
        if (!isNullOrEmpty(options.combosData.workcenter)) {
            resetOrLoad(options.container.workcenter, options.combosData.workcenter);
        }

        //选中默认值
        if (!isNullOrEmpty(options.defaultValue.workcenter)) {
            $("#" + options.container.location).combobox('select', options.defaultValue.workcenter);
        }

        //车间、工位级联仓库
        $("#" + options.container.facility).combobox('options').onSelect = function (r) {
            var d_v = $("#" + options.container.division).combobox('getValue');
            var f_v = $(this).combobox('getValue');
            var url = "/Server/DropDownListResult.aspx?Method=WorkCenterCombobox";
            url = setUrlParam(url, "divisionID", d_v);
            url = setUrlParam(url, "facilityID", f_v);
            url = setUrlParam(url, "Type", options.type);
            url = GetRandURL(url);
            $("#" + options.container.workcenter).combobox("reload", url);
            //清空 
            resetOrLoad(options.container.workcenter);
        };
    }

    //工位,仓库
    if (!isNullOrEmpty(options.container.workcenter) && !isNullOrEmpty(options.container.warehouse)) {
        //加载工位数据
        if (!isNullOrEmpty(options.combosData.workcenter)) {
            resetOrLoad(options.container.workcenter, options.combosData.workcenter);
        }

        //工位选中默认值
        if (!isNullOrEmpty(options.defaultValue.workcenter)) {
            $("#" + options.container.location).combobox('select', options.defaultValue.workcenter);
        }

        //仓库选中默认值
        if (!isNullOrEmpty(options.defaultValue.warehouse)) {
            $("#" + options.container.warehouse).combobox('select', options.defaultValue.warehouse);
        }
        //车间、工位,车间、仓库级联仓库
        $("#" + options.container.facility).combobox('options').onSelect = function (r) {
            var d_v = $("#" + options.container.division).combobox('getValue');
            var f_v = $(this).combobox('getValue');
            var url = "/Server/DropDownListResult.aspx?Method=WorkCenterCombobox";
            url = setUrlParam(url, "divisionID", d_v);
            url = setUrlParam(url, "facilityID", f_v);
            url = setUrlParam(url, "Type", options.type);
            url = GetRandURL(url);
            $("#" + options.container.workcenter).combobox("reload", url);
            //清空工位以及下级级联 
            resetOrLoad(options.container.workcenter);

            url = "/Server/DropDownListResult.aspx?Method=WarehouseCombobox";
            url = setUrlParam(url, "divisionID", d_v);
            url = setUrlParam(url, "facilityID", f_v);
            url = setUrlParam(url, "Type", options.type);
            url = GetRandURL(url);
            $("#" + options.container.warehouse).combobox("reload", url);
            //清空仓库以及下级级联 
            resetOrLoad(options.container.warehouse);
            resetOrLoad(options.container.zone);
            resetOrLoad(options.container.location);
        };
    }
    //工厂
    MakeDivisionCombobox(options);
}